﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ClassBuilder.Comment;

namespace ClassBuilder
{
    public class CSEnum
    {
        /// <summary>
        /// 命名空间
        /// </summary>
        public string NameSpace { get; set; }

        /// <summary>
        /// 枚举名称
        /// </summary>
        public string EnumName { get; set; }


        /// <summary>
        /// 全名称
        /// </summary>
        public string FullName { get { return NameSpace + "." + EnumName; } }


        /// <summary>
        /// 枚举注释
        /// </summary>
        public CSComment Comment { get; set; }

        /// <summary>
        /// 枚举成员
        /// </summary>
        public List<string> Members { get; set; } = new List<string>();

        /// <summary>
        /// 枚举成员与注释
        /// </summary>
        public List<KeyValuePair<string, string>> Member_Comments { get; set; } = new List<KeyValuePair<string, string>>();


        /// <summary>
        /// 命名规则
        /// </summary>
        public NameRule NameRule { get; set; } = NameRule.Normal;


        /// <summary>
        /// 是否有命名空间
        /// </summary>
        private bool IsHaveNS { get; set; }


        public CSEnum(string enumName, string nameSpace = null, CSComment comment = null, NameRule nameRule = NameRule.Normal, params string[] members)
        {
            this.EnumName = enumName?.NameStyle(nameRule);
            this.NameSpace = nameSpace?.NameStyle(nameRule);
            this.Comment = comment;
            this.IsHaveNS = !string.IsNullOrWhiteSpace(this.NameSpace);
            this.NameRule = nameRule;
            if (members != null && members.Length > 0)
            {
                Members.AddRange(members.Select(mbr => mbr?.NameStyle(nameRule)?.KWFormat()));
                Member_Comments.AddRange(members.Select(mbr => new KeyValuePair<string, string>(mbr?.NameStyle(nameRule)?.KWFormat(), string.Empty)));
            }
        }

        /// <summary>
        /// 加入枚举成员与注释
        /// </summary>
        /// <param name="member">枚举成员</param>
        /// <param name="memberSummary">注释</param>
        public void AddMember(string member, string memberSummary = null)
        {
            if (member.NotEmpty() && !Members.Contains(member.NameStyle(NameRule)?.KWFormat()))
            {
                this.Members.Add(member.NameStyle(NameRule)?.KWFormat());
                this.Member_Comments.Add(new KeyValuePair<string, string>(member.NameStyle(NameRule)?.KWFormat(), memberSummary));
            }
        }

        /// <summary>
        /// 加入枚举成员
        /// </summary>
        /// <param name="members">枚举成员</param>
        public void AddMembers(params string[] members)
        {
            if (members != null)
            {
                foreach (var member in members)
                {
                    if (member.NotEmpty() && !Members.Contains(member?.NameStyle(NameRule)?.KWFormat()))
                    {
                        this.Members.Add(member?.NameStyle(NameRule)?.KWFormat());
                        this.Member_Comments.Add(new KeyValuePair<string, string>(member?.NameStyle(NameRule)?.KWFormat(), string.Empty));
                    }
                }
            }
        }

        public string Value
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.EnumName))
                {
                    return string.Empty;
                }
                StringPlus Plus = new StringPlus();
                Plus.AppendLine();

                if (!string.IsNullOrWhiteSpace(NameSpace))
                {
                    Plus.AppendLine($"namespace {NameSpace}");
                    Plus.AppendLine("{");
                }

                if (Comment != null && !string.IsNullOrWhiteSpace(this.Comment.Desc))
                {
                    if (Member_Comments.All(t=>t.Value.IsEmpty()))
                    {
                        var cmt = new CSComment(Comment.Desc);
                        cmt.AddPara(string.Join(" ", Members));
                        foreach (var line in cmt.Lines)
                        {
                            Plus.AppendSpaceLine(IsHaveNS ? 1 : 0, line);
                        }
                    }
                    else
                    {
                        var lstStr = new List<string>();
                        foreach (var item in Member_Comments)
                        {
                            var member = item.Key;
                            var summary = item.Value;
                            lstStr.Add(member + " " + summary.HtmlHandle());
                        }
                        Comment.Items = new ListItems(ItemType.bullet, lstStr.ToArray());
                        foreach (var line in Comment.Lines)
                        {
                            Plus.AppendSpaceLine(IsHaveNS ? 1 : 0, line);
                        }
                    }
                }
                else
                {
                    var cmt = new CSComment(string.Join(" ", Members));
                    foreach (var line in cmt.Lines)
                    {
                        Plus.AppendSpaceLine(IsHaveNS ? 1 : 0, line);
                    }
                }                
                Plus.AppendSpaceLine(IsHaveNS ? 1 : 0, $"public enum {this.EnumName}");
                Plus.AppendSpaceLine(IsHaveNS ? 1 : 0, "{");

                for (int j = 0; j < this.Member_Comments.Count; j++)
                {
                    var member = this.Member_Comments[j].Key;
                    var summary = this.Member_Comments[j].Value.Trim(new char[] { ':' });

                    var lenSpace = (string.IsNullOrWhiteSpace(NameSpace) ? 0 : 1) + 1;

                    if (!string.IsNullOrWhiteSpace(summary))
                    {
                        Plus.AppendSpaceLine(lenSpace, $"/// <summary>");

                        summary = summary.Trim();

                        #region 单行、多行注释
                        var lines = summary.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                        if (lines.Length <= 1)
                        {
                            Plus.AppendSpaceLine(lenSpace, $"/// {summary}");
                        }
                        else
                        {
                            foreach (var line in lines)
                            {
                                Plus.AppendSpaceLine(lenSpace, $"/// {line}");
                            }
                        }
                        #endregion

                        Plus.AppendSpaceLine(lenSpace, $"/// </summary>");
                    }
                    Plus.AppendSpace(lenSpace, $"{member}");

                    if (j != this.Member_Comments.Count - 1)
                    {
                        Plus.AppendLine($",");
                        Plus.AppendLine();
                    }
                }

                Plus.AppendLine();
                Plus.AppendSpaceLine(IsHaveNS ? 1 : 0, "}");
                if (IsHaveNS)
                {
                    Plus.AppendLine("}");
                }
                return Plus.ToString().Trim();
            }
        }

        /// <summary>
        /// 该枚举唯一Id
        /// </summary>
        public string Id
        {
            get
            {
                var strs = Members.ToArray();                
                Array.Sort(strs);
                return string.Join(EnumName, strs).Md5();
            }
        }

        /// <summary>
        /// 枚举包含枚举值的唯一Id
        /// </summary>
        public string MId
        {
            get
            {
                var strs = Members.ToArray();
                Array.Sort(strs);
                return string.Join("", strs).Md5();
            }
        }
    }
}
